package il.ac.braude.sona.gui.forms.questions;

import java.awt.Color;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPasswordField;

/**
 * The QPassword class creates a password test field to fill
 * @author  Ofek Ron
 * @author Avihay Avitan
 * @author Nitzan Inbar
 * @author Sasha Rozenthal
 * Laboratory in Software Engineering,Ort Braude College.</p>
 */
public class QPassword extends FormQuestion {
	private JPasswordField pw = new JPasswordField(20);
	private JLabel q;
	public QPassword(String key) {
		super(BoxLayout.X_AXIS);
		q = new JLabel(key);
		q.setLabelFor(pw);
		add(q);
		add(Box.createHorizontalGlue());
		add(pw);
	}
	@Override
	public void setDefaultAnswer(Object defaultAnswer) {
	}

	@Override
	public Object getAnswer() {
		String res = new String(pw.getPassword());
		pw.setText("");
		return (res.length()==0 && isNecessary()) ? null : res;
	}
	@Override
	public void markQuestion() {
		q.setForeground(Color.red);
		this.requestFocus();
	}
	@Override
	public void unmarkQuestion() {
		q.setForeground(Color.black);
	}
}
